#include "aigame.h"
#include<QDebug>

AIgame::AIgame(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1000,800);
    this->setWindowIcon(QPixmap(":/res/R-C.jpg"));//程序app图片
    this->setWindowTitle("对局进行中");//title

    games=new Gamestart;//创建第二个页面对象
    //退出按钮
    QPushButton* exitbtn=new QPushButton("退出对局",this);
    exitbtn->setFixedSize(100,40);
    exitbtn->setFont(QFont("SimHei",12,QFont::Bold));
    exitbtn->move(850,650);
    connect(exitbtn,QPushButton::clicked,[=](){
        int res=QMessageBox::question(this, "GAME OVER", "是否退出当前对局？", QMessageBox::Yes,QMessageBox::No);
        if(res==QMessageBox::Yes)
        {
            emit game_back();
            games->game_init();
        }
    });

    server=new QWebSocketServer("Server",QWebSocketServer::NonSecureMode,this);
    server->listen(QHostAddress::Any,8888);
    connect(server,&QWebSocketServer::newConnection,this,&AIgame::onNewConnection);
}
void AIgame::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/new/prefix1/res/9802.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/new/prefix1/res/19101659901796309955316845.jpg");
    pix=pix.scaled(650,650);
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,this->height()*0.5-pix.height()*0.5,pix);

    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(games->chess[i][j]==-1)
            {
                pix.load(":/res/white.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
            else if(games->chess[i][j]== 1)
            {
                pix.load(":/res/black.png");
                pix=pix.scaled(30,30);
                painter.drawPixmap(this->width()*0.5-650*0.5+10+42.85*i,this->height()*0.5-650*0.5+10+j*42.85,pix);
            }
        }
    }
}
void AIgame::mousePressEvent(QMouseEvent *event)
{
    int x=event->x();
    int y=event->y();
    if(x>=this->width()*0.5-300&&x<=this->width()*0.5+300&&y>=this->height()*0.5-300&&y<=this->height()*0.5+300)
    {
       double m=(x-this->width()*0.5+300)/42.85;
       double n=(y-this->height()*0.5+300)/42.85;
       int i=(x-this->width()*0.5+300)/42.85;
       int j=(y-this->height()*0.5+300)/42.85;

       if(m-double(i)>=0.7)
           i++;
       else if(m-double(i)<=0.3)
           i=i;
       else
           return;

       if(n-double(j)>=0.7)
           j++;
       else if(n-double(j)<=0.3)
           j=j;
       else
           return;

       this->x=i;
       this->y=j;
       //this->temp=games->chess_status;

       int n1=(this->x*100+this->y)*10;
       QByteArray send_data;
       send_data.setNum(n1);
       emit sendMessage(send_data);


           if(games->chess[i][j]==0)
           {
             games->chess[i][j]=1;
             update();
             if(this->winorn(i,j))
             {
                 int res=QMessageBox::information(this, "GAME OVER", "恭喜您获胜！", QMessageBox::Ok);
                 if(res==QMessageBox::Ok)
                 {

                     QByteArray send_data2;
                     send_data2.setNum(3);
                     emit sendMessage(send_data2);

                     emit game_back();
                     return;

                 }
             }
             this->AI_chess();
           }
           else
               return;




    }
}

void AIgame::AI_chess()
{
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {

            this->games->game_score[i][j]=0;
        }

    }
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(this->games->chess[i][j]!=1||this->games->chess[i][j]!=-1)
            this->get_score(i,j);
        }
    }
    int max_score=this->games->game_score[0][0];
    int k=0,t=0;



    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(this->games->game_score[i][j]>max_score&&this->games->chess[i][j]==0)
            {
                max_score=this->games->game_score[i][j];
                k=i;
                t=j;
            }
        }
    }

    this->games->chess[k][t]=-1;
    this->update();
    int n1=(k*100+t)*10+2;
    QByteArray send_data;
    send_data.setNum(n1);
    emit sendMessage(send_data);
    if(this->winorn(k,t))
    {
        int res=QMessageBox::information(this, "GAME OVER", "很遗憾，您输了！", QMessageBox::Ok);
        if(res==QMessageBox::Ok)
        {
            emit game_back();
            QByteArray send_data2;
            send_data2.setNum(4);
            emit sendMessage(send_data2);
            return;

        }
    }

}

void AIgame::get_score(int x0, int y0)
{
    int enemy=0;
    int empty=0;
    //纵向
    for(int i=1;i<=4;i++)
    {
        if((x0+i)<15&&this->games->chess[x0+i][y0]==1)
            enemy++;
        else if((x0+i)<15&&this->games->chess[x0+i][y0]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((x0-i)>=0&&this->games->chess[x0-i][y0]==1)
            enemy++;
        else if((x0-i)>=0&&this->games->chess[x0-i][y0]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=10;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=50;
        else if(empty==2)
            this->games->game_score[x0][y0]+=70;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=150;
        else if(empty==2)
            this->games->game_score[x0][y0]+=6000;
    }
    else if(enemy==4)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=5000;
        else if(empty==2)
            this->games->game_score[x0][y0]+=10000;
    }
    else if(enemy>=5)
    {
        this->games->game_score[x0][y0]+=20000;
    }

    //横向
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&this->games->chess[x0][y0+i]==1)
            enemy++;
        else if((y0+i)<15&&this->games->chess[x0][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&this->games->chess[x0][y0-i]==1)
            enemy++;
        else if((y0-i)>=0&&this->games->chess[x0][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=10;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=50;
        else if(empty==2)
            this->games->game_score[x0][y0]+=70;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=150;
        else if(empty==2)
            this->games->game_score[x0][y0]+=6000;
    }
    else if(enemy==4)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=5000;
        else if(empty==2)
            this->games->game_score[x0][y0]+=10000;
    }
    else if(enemy>=5)
    {
        this->games->game_score[x0][y0]+=20000;
    }

    //对角线1
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&(x0+i)<15&&this->games->chess[x0+i][y0+i]==1)
            enemy++;
        else if((y0+i)<15&&(x0+i)<15&&this->games->chess[x0+i][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&(x0-i)>=0&&this->games->chess[x0-i][y0-i]==1)
            enemy++;
        else if((y0-i)>=0&&(x0-i)>=0&&this->games->chess[x0-i][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=10;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=50;
        else if(empty==2)
            this->games->game_score[x0][y0]+=70;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=150;
        else if(empty==2)
            this->games->game_score[x0][y0]+=6000;
    }
    else if(enemy==4)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=5000;
        else if(empty==2)
            this->games->game_score[x0][y0]+=10000;
    }
    else if(enemy>=5)
    {
        this->games->game_score[x0][y0]+=20000;
    }

    //对角线2
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&(x0-i)>=0&&this->games->chess[x0-i][y0+i]==1)
            enemy++;
        else if((y0+i)<15&&(x0-i)>=0&&this->games->chess[x0-i][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&(x0+i)<150&&this->games->chess[x0+i][y0-i]==1)
            enemy++;
        else if((y0-i)>=0&&(x0+i)<15&&this->games->chess[x0+i][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=10;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=50;
        else if(empty==2)
            this->games->game_score[x0][y0]+=70;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=150;
        else if(empty==2)
            this->games->game_score[x0][y0]+=6000;
    }
    else if(enemy==4)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=5000;
        else if(empty==2)
            this->games->game_score[x0][y0]+=10000;
    }
    else if(enemy>=5)
    {
        this->games->game_score[x0][y0]+=20000;
    }

    //自检
    //纵向
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((x0+i)<15&&this->games->chess[x0+i][y0]==-1)
            enemy++;
        else if((x0+i)<15&&this->games->chess[x0+i][y0]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((x0-i)>=0&&this->games->chess[x0-i][y0]==-1)
            enemy++;
        else if((x0-i)>=0&&this->games->chess[x0-i][y0]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=15;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=60;
        else if(empty==2)
            this->games->game_score[x0][y0]+=85;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=160;
        else if(empty==2)
            this->games->game_score[x0][y0]+=2000;
    }
    else if(enemy>=4)
    {

            this->games->game_score[x0][y0]+=21000;
    }


    //横向
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&this->games->chess[x0][y0+i]==-1)
            enemy++;
        else if((y0+i)<15&&this->games->chess[x0][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&this->games->chess[x0][y0-i]==-1)
            enemy++;
        else if((y0-i)>=0&&this->games->chess[x0][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=15;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=60;
        else if(empty==2)
            this->games->game_score[x0][y0]+=85;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=160;
        else if(empty==2)
            this->games->game_score[x0][y0]+=2000;
    }
    else if(enemy>=4)
    {

            this->games->game_score[x0][y0]+=21000;
    }

    //对角线1
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&(x0+i)<15&&this->games->chess[x0+i][y0+i]==1)
            enemy++;
        else if((y0+i)<15&&(x0+i)<15&&this->games->chess[x0+i][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&(x0-i)>=0&&this->games->chess[x0-i][y0-i]==1)
            enemy++;
        else if((y0-i)>=0&&(x0-i)>=0&&this->games->chess[x0-i][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=15;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=60;
        else if(empty==2)
            this->games->game_score[x0][y0]+=85;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=160;
        else if(empty==2)
            this->games->game_score[x0][y0]+=2000;
    }
    else if(enemy>=4)
    {

            this->games->game_score[x0][y0]+=21000;
    }
    //对角线2
    enemy=0;
    empty=0;
    for(int i=1;i<=4;i++)
    {
        if((y0+i)<15&&(x0-i)>=0&&this->games->chess[x0-i][y0+i]==-1)
            enemy++;
        else if((y0+i)<15&&(x0-i)>=0&&this->games->chess[x0-i][y0+i]==0)
        {
            empty++;
            break;
        }
        else
            break;

    }
    for(int i=1;i<=4;i++)
    {
        if((y0-i)>=0&&(x0+i)<150&&this->games->chess[x0+i][y0-i]==-1)
            enemy++;
        else if((y0-i)>=0&&(x0+i)<15&&this->games->chess[x0+i][y0-i]==0)
        {
            empty++;
            break;
        }
        else
            break;
    }

    if(enemy==1)
    this->games->game_score[x0][y0]+=15;
    else if(enemy==2)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=60;
        else if(empty==2)
            this->games->game_score[x0][y0]+=85;
    }
    else if(enemy==3)
    {
        if(empty==1)
            this->games->game_score[x0][y0]+=160;
        else if(empty==2)
            this->games->game_score[x0][y0]+=2000;
    }
    else if(enemy>=4)
    {

            this->games->game_score[x0][y0]+=21000;
    }
}


bool AIgame::winorn(int i,int j)
{
    int n=0;
    int k,l;
    k=i;
    l=j;
    //检测横向
    while (k>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        n++;
    }
    k=i+1;
    while (k<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        n++;
    }
    if(n>=5)
        return true;
    //检测纵向
    k=i;
    n=0;
    while (l>-1&&games->chess[k][l]==games->chess[i][j]) {
        l--;
        n++;
    }
    l=j+1;
    while (l<16&&games->chess[k][l]==games->chess[i][j]) {
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测左上左下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l--;
        n++;
    }
    k=i+1;
    l=j+1;
    while (k<16&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l++;
        n++;
    }
    if(n>=5)
        return true;
    //检测右上右下
    k=i;
    l=j;
    n=0;
    while (k>-1&&l<16&&games->chess[k][l]==games->chess[i][j]) {
        k--;
        l++;
        n++;
    }
    k=i+1;
    l=j-1;
    while (k<16&&l>-1&&games->chess[k][l]==games->chess[i][j]) {
        k++;
        l--;
        n++;
    }
    if(n>=5)
        return true;


    return false;

}

void AIgame::onNewConnection()
{
    socket=server->nextPendingConnection();
    if(!socket)
        return;

    connect(this,&AIgame::sendMessage,socket,&QWebSocket::sendTextMessage);
    //断开连接，释放
    connect(socket,&QWebSocket::disconnected,[this](){

        socket->deleteLater();
    });
}
